﻿using BarManager.Core.BusinessRules.Interfaces.Specification;
using BarManager.Core.DomainService;
using BarManager.Core.Entity;
using System.Collections.Generic;

namespace BarManager.Core.BusinessRules.Specification
{
    public class CargoAddSpecification : BaseSpecification<Cargo>, ICargoSpecification
    {
        private Cargo Cargo = null;

        public CargoAddSpecification(ICargoRepository repository)
            : base(repository)
        {
        }

        public override bool IsSatisfiedBy(Cargo entity)
        {
            if (entity.Id == 0)
                Cargo = BaseRepository.Get(x => x.Descripcion == entity.Descripcion);
            else
                Cargo = BaseRepository.Get(x => x.Descripcion == entity.Descripcion && x.Id != entity.Id);
            return (Cargo == null);
        }

        public override IEnumerable<string> GetErrorMessage()
        {
            if (Cargo != null) yield return string.Format("Ya existe un cargo con el nombre: {0}", Cargo.Descripcion);
            yield break;
        }
    }
}
